from django.http import HttpResponse
from django.shortcuts import render
from .models import Girl, Boy


# Create your views here.
def boy_add(request):
    name = "jack"
    age = 20
    boy = Boy(name=name, age=age)
    boy.save()

    return HttpResponse("boy add ok")


def boy_delete(request):
    # 删除boy数据
    Boy.objects.get(id=1).delete()
    return HttpResponse("boy delete ok")


def girl_add(request):
    name = "xiaohua"
    age = 20
    # girl = Girl(name=name, age=age, boy_id=2)
    boy = Boy.objects.filter(id=2).first()
    # 添加数据时  boy参数也可以直接写对象
    girl = Girl(name=name, age=age, boy=boy)
    girl.save()
    return HttpResponse("girl add ok")


def girl_delete(request):
    # 删除
    Girl.objects.filter(id=7).delete()
    return HttpResponse("girl delete ok")

"""
一对一的关系中  查询操作时 支持正反向关联
"""
def girl_select(request):
    girl = Girl.objects.filter(id=9).first()
    # 自动关联查询  正向关联
    boy_name = girl.boy.name
    boy_age = girl.boy.age
    print(boy_name, boy_age)
    return HttpResponse("girl select ok")


def boy_select(request):
    boy = Boy.objects.filter(id=2).first()
    # 自动关联查询  反向关联
    girl_name = boy.girl.name
    girl_age = boy.girl.age
    print(girl_name, girl_age)

    return HttpResponse("girl select ok")
